<?php
// +----------------------------------------------------------------------
// |  [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\meeting\controller;

use cmf\controller\AdminBaseController;
use function Sodium\increment;
use think\Db;

class RoomController extends AdminBaseController
{

    /*
     * 展位列表
     * */
    public function index()
    {
        if (input('meeting_id')) {
            session('MEETING_ID', input('meeting_id'));
        }
        // 展馆
        $showroom = Db::table('ims_hc_meet_showroom')
            ->where('meeting_id', session('MEETING_ID'))
            ->order('title')
            ->select()->toArray();

        $where   = [];
        $where[] = ['number', 'like', '%' . input('number', '') . '%'];

        if (session('BUILDER_ID')) {
            $where[] = ['builder_id', '=', session('BUILDER_ID')];
        }

        $data = Db::table('ims_hc_meet_room')
            ->where($where)
            ->paginate(10);
        $page = $data->render();
        $list = $data->toArray();

        $this->assign('list', $list['data']);
        $this->assign('page', $page);
        $this->assign('showroom', $showroom);
        return $this->fetch();
    }


    /*
     * 关联展位
     * @param
     * $return
     */
    public function bindPost()
    {
        $room_id = input('room_id', 0);
        if (empty($room_id)) $this->error('参数错误');
        $data                    = [];
        $data['is_sure']         = 2; // 1 待审核 2 已确认
        $data['two']             = input('two', 0);
        $data['two_area']        = input('two_area', 0);
        $data['exhibitor_name']  = input('exhibitor_name', '');
        $data['exhibitor_phone'] = input('exhibitor_phone', '');
        $data['linkman']         = input('linkman', '');
        $data['builder_id']      = session('BUILDER_ID');
        $data['builder_name']    = input('builder_name', '');
        $data['builder_phone']   = input('builder_phone', '');
        $data['company']         = input('company', '');
        $data['bank_name']       = input('bank_name', '');
        $data['bank_no']         = input('bank_no', '');
        if (empty($data['exhibitor_name']) || empty($data['exhibitor_phone']) || empty($data['builder_id']) || empty($data['builder_name']) || empty($data['builder_phone']) || empty($data['company']) || empty($data['bank_name']) || empty($data['bank_no']))
            $this->error('缺少必要数据');
        $data['add_time']    = time();
        $data['update_time'] = time();

        $info = Db::table('ims_hc_meet_room')->find($room_id);

        if ($info && $info['builder_id'] && $info['builder_id'] != $data['builder_id']) {
            $this->error('展位已被关联');
        }

        $re = Db::table('ims_hc_meet_room')
            ->where('room_id', $room_id)
            ->update($data);

        if ($re) {
            (new OrderController())->order($room_id);
        }

        $this->success('您已关联展位，主办方正在审核中…待审核成功后，请进行报图选项。', 'index');
    }


    /*
     * 申报-》
     * picture
     * @param
     * $return
     */
    public function picture($room_id)
    {
        $list = Db::table('ims_hc_meet_room_check_picture')
            ->where('meeting_id', session('MEETING_ID'))
            ->where('room_id', $room_id)
            ->where('builder_id', session('BUILDER_ID'))
            ->find();
        if ($list) {
            $list['content']  = empty($list['content']) ? [] : json_decode($list['content'], 1);
            $list['content2'] = empty($list['content2']) ? [] : json_decode($list['content2'], 1);
            $list['content3'] = empty($list['content3']) ? [] : json_decode($list['content3'], 1);
            $list['content4'] = empty($list['content4']) ? [] : json_decode($list['content4'], 1);
            $list['content5'] = empty($list['content5']) ? [] : json_decode($list['content5'], 1);
            $list['content6'] = empty($list['content6']) ? [] : json_decode($list['content6'], 1);
            $list['content7'] = empty($list['content7']) ? [] : json_decode($list['content7'], 1);
        }

        $list['remark']  = Db::table('ims_hc_meet')
            ->where('id', session('MEETING_ID'))
            ->value('remark');
        $list['remark2'] = Db::table('ims_hc_meet')
            ->where('id', session('MEETING_ID'))
            ->value('remark2');

        $this->assign('list', $list);
        // 进度
        if ($list && isset($list['status'])) {
            if ($list['status'] == 0 || $list['status'] == 2) {
                $w = 55;
            } else {
                $w = 100;
            }
        } else {
            $w = 20;
        }

        $this->assign('w', $w);
        $this->assign('room_id', $room_id);

        return $this->fetch();

    }

    /*
     * 资料上传
     * @param
     * $return
     */
    public function picturePost()
    {
        $room_id = input('room_id');

        $room = Db::table('ims_hc_meet_room')
            ->where('room_id', $room_id)
            ->find();
        $info = Db::table('ims_hc_meet_room_check_picture')
            ->where('meeting_id', $room['meeting_id'])
            ->where('room_id', $room_id)
            ->where('builder_id', session('BUILDER_ID'))
            ->find();

        $data    = [];
        $content = input('content', '');
        if (empty($content)) {
            $this->error('#1 不能为空');
        }
        $data['content'] = $content ? json_encode($content, 1) : '';
        $content2        = input('content2', '');
        if (empty($content2)) {
            $this->error('#2 不能为空');
        }
        $data['content2'] = $content2 ? json_encode($content2, 1) : '';
        $content3         = input('content3', '');
        if (empty($content3)) {
            $this->error('#3 不能为空');
        }
        $data['content3'] = $content3 ? json_encode($content3, 1) : '';
        $content4         = input('content4', '');
        if (empty($content4)) {
            $this->error('#4 不能为空');
        }
        $data['content4'] = $content4 ? json_encode($content4, 1) : '';
        $content5         = input('content5', '');
        if (empty($content5)) {
            $this->error('#5 不能为空');
        }
        $data['content5'] = $content5 ? json_encode($content5, 1) : '';
        $content6         = input('content6', '');
        if (empty($content6)) {
            $this->error('#6 不能为空');
        }
        $data['content6'] = $content6 ? json_encode($content6, 1) : '';
        $content7         = input('content7', '');

        $data['content7'] = $content7 ? json_encode($content7, 1) : '';

        if (empty($info)) {
            $data['status']      = 1; // 0 待审核 1 已通过
            $data['room_id']     = $room_id;
            $data['number']      = $room['number'];
            $data['builder_id']  = session('BUILDER_ID');
            $data['meeting_id']  = session('MEETING_ID');
            $data['create_time'] = time();
            $builder             = $this->builderInfo($data['builder_id']);
            $data['uid']         = $builder['uid'];
            $re                  = Db::table('ims_hc_meet_room_check_picture')->insertGetId($data);

        } else {
            $data['status'] = 1; // 0 待审核 1 已通过
            Db::table('ims_hc_meet_room_check_picture')
                ->where('meeting_id', $room['meeting_id'])
                ->where('room_id', $room_id)
                ->where('builder_id', session('BUILDER_ID'))
                ->update($data);
            $re = 1;
        }

        if ($re) {
            $this->success('成功', url('meeting/room/index'));
        } else {
            $this->error('失败');
        }

    }

    /*
     * 格式化数据
     * @param
     * $return
     */
    public function saveData()
    {
        $data = input();
        return json(['data' => $data]);
    }

    /*
     * 查询展位列表
     * @param
     * $return
     */
    public function roomList($showroom_id)
    {
        $room = Db::table('ims_hc_meet_room')
            ->where('meeting_id', session('MEETING_ID'))
            ->where('showroom_id', $showroom_id)
            ->where('builder_id', '=', 0)
            ->order('number')
            ->select()->toArray();

        $this->success('成功', '', $room);
    }

    /*
     * 查询展位详情
     * @param
     * $return
     */
    public function roomInfo($room_id)
    {
        $info = Db::table('ims_hc_meet_room')
            ->where('room_id', $room_id)
            ->find();
        $this->success('成功', '', $info);
    }

    /*
     * 搭建商信息
     * @param
     * $return
     */
    public function builderInfo($builder_id)
    {
        if ($builder_id) {
            $where[] = ['builder_id', '=', $builder_id];
        } else {
            $where[] = ['builder_id', '=', session('BUILDER_ID')];
        }
        return Db::table('ims_hc_meet_builder')
            ->where($where)
            ->find();
//        $this->success('成功','',$info);
    }


    /*
     * 发票
     * bill
     * @param
     * $return
     */
    public function bill()
    {

        $room_id   = input('room_id');
        $room_info = Db::table('ims_hc_meet_room')
            ->where('room_id', $room_id)
            ->find();

        $builder_info = Db::table('ims_hc_meet_builder')
            ->where('builder_id', session('BUILDER_ID'))
            ->find();

        // 特装、审图
        $fee   = Db::table('ims_hc_meet_fee')
            ->where('room_id', '=', $room_id)
            ->where('type', '<>', 1)
            ->find();
        $data  = [];
        $total = 0;
        if ($fee && $fee['remark']) {
            $remark = json_decode($fee['remark'], 1);

            foreach ($remark as $k => $v) {
                if ($v['name'] == '施工押金') {
                    unset($remark[$k]);
                } else {
                    $v['number'] = 1;
                    $v['total']  = $v['val'];
                    $data[]      = $v;
                    $total       += $v['total'];
                }
            }
        }

        // 租赁订单
        $where = [
            'a.uid'        => $builder_info['uid'],
            'a.meeting_id' => session('MEETING_ID'),
            'a.room_id'    => $room_id,
            'a.status'     => 2,
        ];
        $order = Db::table('ims_hc_meet_order')
            ->alias('a')
            ->field('a.*,b.hire_id,b.hire_name,b.class_id,b.class_name,b.price goods_price,b.deposit goods_deposit,b.number goods_number')
            ->leftJoin('mp_dehomic_com.ims_hc_meet_order_goods b', 'a.order_no=b.order_no')
            ->where($where)
            ->where('a.price', '>', 0)
            ->select()
            ->toArray();

        if ($order) {
            foreach ($order as $v) {
                $data[] = [
                    'name'   => $v['hire_name'],
                    'val'    => $v['goods_price'],
                    'number' => $v['goods_number'],
                    'total'  => $v['price'],
                ];
                $total  += $v['price'];
            }
        }
        $bill = Db::table('ims_hc_meet_bill')
            ->where('room_id', $room_id)
            ->find();

        $this->assign('info', $room_info);
        $this->assign('bill', $bill);
        $this->assign('builder_info', $builder_info);
        $this->assign('data', $data);
        $this->assign('total', $total);
        return $this->fetch();
    }

    /*
     * saveBill
     * @param  
     * $return 
     */
    public function saveBill()
    {
        $data = input();

        if (!isset($data['type'])) {
            $this->error('请选择开票方式');
        } else {
            if ($data['type'] && empty($data['email'])) $this->error('请填写电子邮箱');
            if (!$data['type'] && empty($data['address'])) $this->error('请填写收件地址');
        }

        $info = Db::table('ims_hc_meet_bill')
            ->where('room_id', $data['room_id'])
            ->find();

        $data['status'] = 0;
        if ($info) {
            Db::table('ims_hc_meet_bill')
                ->where('id', $info['id'])
                ->update($data);
        } else {
            $data['create_time'] = time();
            Db::table('ims_hc_meet_bill')
                ->insert($data);
        }

        $this->success('申请成功');
    }

    /*
     * 押金退款
     * refund
     * @param
     * $return
     */
    public function refund()
    {

        $builder_info = Db::table('ims_hc_meet_builder')
            ->where('builder_id', session('BUILDER_ID'))
            ->find();

        $data  = [];
        $total = 0;
        if ($builder_info['is_recommend']) {
            // 推荐搭建商
            $fee = Db::table('ims_hc_meet_fee')
                ->where('type', '=', 1)
                ->where('builder_id', '=', session('BUILDER_ID'))
                ->where('meeting_id', '=', session('MEETING_ID'))
                ->find();
            if ($fee) {
                $data[] = [
                    'name'   => '施工押金',
                    'val'    => $fee['total'],
                    'number' => 1,
                    'total'  => $fee['total'],
                ];
                $total = $fee['total'];
            }
        } else {
            // 非推荐搭建商
            $fee = Db::table('ims_hc_meet_fee')
                ->where('type', '<>', 1)
                ->where('builder_id', '=', session('BUILDER_ID'))
                ->where('meeting_id', '=', session('MEETING_ID'))
                ->select();
            if ($fee) {
                foreach ($fee as $value){
                    if($value['remark']){
                        $remark = json_decode($value['remark'], 1);

                        foreach ($remark as $k => $v) {
                            if ($v['name'] == '施工押金') {
                                $v['number'] = 1;
                                $v['total']  = $v['val'];
                                $data[]      = $v;
                                $total       += $v['total'];
                            }
                        }
                    }
                }
            }
        }


        $info = Db::table('ims_hc_meet_refund')
            ->where('builder_id', '=', session('BUILDER_ID'))
            ->where('meeting_id', '=', session('MEETING_ID'))
            ->find();



        $this->assign('info', $info);
        $this->assign('builder_info', $builder_info);
        $this->assign('data', $data);
        $this->assign('total', $total);
        return $this->fetch();
    }

    /*
         * saveRefund
         * @param
         * $return
         */
    public function saveRefund()
    {
        $data = input();

        $info = Db::table('ims_hc_meet_refund')
            ->where('builder_id', '=', session('BUILDER_ID'))
            ->where('meeting_id', '=', session('MEETING_ID'))
            ->find();
        $data['status'] = 0;
        if ($info) {
            Db::table('ims_hc_meet_refund')
                ->where('id', $info['id'])
                ->update($data);
        } else {
            $data['create_time'] = time();
            Db::table('ims_hc_meet_refund')
                ->insert($data);
        }

        $this->success('申请成功');
    }
}